package datastructure.array;

/**
 * @author XY
 * @version 1.0
 * @date 2022/3/2 9:11
 * @Description
 */
public class Leet27 {
    public static int removeElement(int[] nums, int val) {
        int n =nums.length;
        for (int i = 0; i < n; i++) {
            if(nums[i]==val){
                for(int j =i;j<n-1;j++){
                    nums[j]=nums[j+1];
                }
                i--;
                n--;
            }
        }
        return n;
    }

    //双指针
    public static int removeElement1(int[] nums, int val) {
        int n =nums.length;
        int left = 0;
        for(int i =0;i<n;i++){
            if(nums[i]!=val){
                nums[left] = nums[i];
                left++;
            }
        }
        return left;
    }

    public static void main(String[] args) {
        int[] arr = {0,1,2,2,3,0,4,2};
        System.out.println(removeElement1(arr, 2));
    }
}
